package cn.iocoder.yudao.module.exam.controller.admin.questionoption;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.exam.controller.admin.questionoption.vo.*;
import cn.iocoder.yudao.module.exam.dal.dataobject.questionoption.QuestionOptionDO;
import cn.iocoder.yudao.module.exam.service.questionoption.QuestionOptionService;

@Tag(name = "管理后台 - 试题选项")
@RestController
@RequestMapping("/exam/question-option")
@Validated
public class QuestionOptionController {

    @Resource
    private QuestionOptionService questionOptionService;

    @PostMapping("/create")
    @Operation(summary = "创建试题选项")
    @PreAuthorize("@ss.hasPermission('exam:question-option:create')")
    public CommonResult<Long> createQuestionOption(@Valid @RequestBody QuestionOptionSaveReqVO createReqVO) {
        return success(questionOptionService.createQuestionOption(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新试题选项")
    @PreAuthorize("@ss.hasPermission('exam:question-option:update')")
    public CommonResult<Boolean> updateQuestionOption(@Valid @RequestBody QuestionOptionSaveReqVO updateReqVO) {
        questionOptionService.updateQuestionOption(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除试题选项")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('exam:question-option:delete')")
    public CommonResult<Boolean> deleteQuestionOption(@RequestParam("id") Long id) {
        questionOptionService.deleteQuestionOption(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得试题选项")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('exam:question-option:query')")
    public CommonResult<QuestionOptionRespVO> getQuestionOption(@RequestParam("id") Long id) {
        QuestionOptionDO questionOption = questionOptionService.getQuestionOption(id);
        return success(BeanUtils.toBean(questionOption, QuestionOptionRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得试题选项分页")
    @PreAuthorize("@ss.hasPermission('exam:question-option:query')")
    public CommonResult<PageResult<QuestionOptionRespVO>> getQuestionOptionPage(@Valid QuestionOptionPageReqVO pageReqVO) {
        PageResult<QuestionOptionDO> pageResult = questionOptionService.getQuestionOptionPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, QuestionOptionRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出试题选项 Excel")
    @PreAuthorize("@ss.hasPermission('exam:question-option:export')")
    @OperateLog(type = EXPORT)
    public void exportQuestionOptionExcel(@Valid QuestionOptionPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<QuestionOptionDO> list = questionOptionService.getQuestionOptionPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "试题选项.xls", "数据", QuestionOptionRespVO.class,
                        BeanUtils.toBean(list, QuestionOptionRespVO.class));
    }

}